const db = require("../db/index");
const axios = require('axios');

// 配置百度AI参数
const apiKey  = 'A2oTmSCrnQphXMFsaw89jGGM';
const secretKey  = 'yOpUTfcRRyxzu8NY4FwZAaCPz2P8Uvw2B';

// 获取access_token的函数
const getAccessToken= async () => {
    const response = await axios.post('https://aip.baidubce.com/oauth/2.0/token', null, {
        params: {
            grant_type: 'client_credentials',
            client_id: apiKey,
            client_secret: secretKey
        }
    });
    return response.data.access_token;
}
// console.log(getAccessToken(),88888)

// 发送请求到UNIT智能对话API的函数
const  sendMessageToUnit = async (message) => {
    const accessToken = await getAccessToken();
    const response = await axios.post(`https://aip.baidubce.com/rpc/2.0/unit/bot/chat`, null, {
        headers: {
            'Content-Type': 'application/json'
        },
        params: {
            access_token: accessToken
        },
        data: {
            message: message,
            user_id: 'your_user_id' // 可以是任意唯一标识用户的字符串
        }
    });
    return response.data.result;
}


// 设置Express路由
// exports.chatapi = async (req, res) => {
//     const { message } = req.body;
//     axios.post('https://aip.baidubce.com/rpc/2.0/unit/bot/chat')
// };